Fuente: Reportes de la Verificación Nacional Muestral (2005-2018). DERFE-Dirección de Estadística.

Gráfica 1

Row

Indicadores de actualización a nivel nacional y estatal

Gráfica 2

Row

Evolución de los indicadores de actualización, a nivel nacional y estatal

Gráfica 3

Row

Indicadores de actualización por año, y por entidad federativa

---
title: "Verificación Nacional Muestral - Actualización. Periodo: 2005-2018"
author: "DERFE-Dirección de Estadística"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    source_code: embed
    logo: ine_logo2.png
---

Fuente: Reportes de la Verificación Nacional Muestral (2005-2018). DERFE-Dirección de Estadística.

```{r setup, include=FALSE}
library(ggplot2)
library(plotly)
library(tidyverse)
library(flexdashboard)


#----------------------------------------------------------------------------------#
################################# Paquetes y setup #################################
#----------------------------------------------------------------------------------#

library(pacman)
p_load(tidyverse,
       lubridate,
       plotly,
       ggthemes,
       RColorBrewer)

#Prevenir notación científica
options(scipen=999) 
#Getting the path of your current open file
#current_path = rstudioapi::getActiveDocumentContext()$path 
#setwd(dirname(current_path))
#print(getwd())



#----------------------------------------------------------------------------------#
############################# Datos originales #####################################
#----------------------------------------------------------------------------------#

Datos <- read_csv("Datos_VNM_Actualizacion_Proc.csv",
                  locale = locale(encoding = 'latin1'),
                  col_types = cols(Indicador = col_double(), 
                                                 Limite_Inferior = col_double(), 
                                                 Limite_Superior = col_double(),
                                                 `Fecha` = col_date(format = "%Y-%m-%d")))

#convertir a factor la columna estados
Datos$Edo <- as.factor(Datos$Edo)

#convertir a factor la columna tramite
Datos$Nombre_Indicador <- as.factor(Datos$Nombre_Indicador)

#View(Datos)


#----------------------------------------------------------------------------------#
############################# transformacion de datos ##############################
#----------------------------------------------------------------------------------#

Datos2 <- Datos %>% filter(Edo != 'NACIONAL')
Datos2$Edo <- as.character(Datos2$Edo)
Datos2$Edo <- as.factor(Datos2$Edo)


#----------------------------------------------------------------------------------#
############################### opciones estéticas #################################
#----------------------------------------------------------------------------------#

f <- list(
  size = 14,
  color = "#7f7f7f"
)

xx <- list(
  title = "Fecha",
  titlefont = f
)

xx2 <- list(
  title = "Entidad",
  titlefont = f
)

yy <- list(
  title = "Porcentaje (%)",
  titlefont = f
)

l <- list(
  font = list(
    size = 8,
    color = "#000"),
  orientation = 'v',
  x = -400, 
  y = 0.5)

l1 <- list(
  font = list(
    size = 8,
    color = "#000"),
  orientation = 'h')


# funcion para generar la lista de opciones del menu 
#(ver: https://stackoverflow.com/questions/55075168/loop-plotly-menu)

get_menu_list <- function(names){
  n_names = length(names)
  buttons = vector("list",n_names)
  
  for(i in seq_along(buttons)){
    buttons[i] = list(list(method = "restyle",
                           args = list("transforms[0].value", names[i]),
                           label = names[i]))
  }
  
  return_list = list(
    list(
      type = 'dropdown',
      active = 0,
      buttons = buttons
    )
  )
  
  return(return_list)
}


```

Gráfica 1
=======================================================================

Row
-----------------------------------------------------------------------

### Indicadores de actualización a nivel nacional y estatal

```{r}
fig <- Datos %>% 
  plot_ly(
    x = ~Fecha, 
    y = ~Indicador,
    customdata = ~Edo,
    color = ~Nombre_Indicador,
    text = ~paste(Edo,
                  '
Año:', year(Fecha), '
Indicador:', Nombre_Indicador, '
Porcentaje:', round(Indicador,2),'%', '
Limite inferior:', round(Limite_Inferior,2),'%', '
Limite superior:', round(Limite_Superior,2),'%'), hoverinfo = 'text', transforms = list( list( type = 'filter', target = 'customdata', operation = '=', value = unique(Datos$Edo)[1] ) )) %>% add_trace(type = 'scatter', mode='lines+markers', visible = 'legendonly' )%>% layout(xaxis = xx, yaxis = yy, legend = list( font = list( size = 10, color = "#000"), orientation = 'h', x = 0.0, y = -0.2), updatemenus = list( list(direction = "right", xanchor = 'right', yanchor = "top", x = 0.1, y= 1.1, font = list( size = 10), type = 'dropdown', active = 0, buttons = apply(as.data.frame(as.factor(unique(Datos$Edo))), 1, function(x) list(method = 'restyle',args = list('transforms[0].value',x),label = x))) ) ) fig ``` Gráfica 2 ======================================================================= Row ----------------------------------------------------------------------- ### Evolución de los indicadores de actualización, a nivel nacional y estatal ```{r} fig1 <- Datos %>% plot_ly(x = ~Fecha, y = ~Indicador, color = ~factor(Edo, levels=as.character(unique(Datos$Edo))), customdata = ~Nombre_Indicador, hoverinfo = 'text', text = ~paste(Edo, '
Año:', year(Fecha), '
Indicador:', Nombre_Indicador, '
Porcentaje:', round(Indicador,2),'%', '
Limite inferior:', round(Limite_Inferior,2),'%', '
Limite superior:', round(Limite_Superior,2),'%'), transforms = list( list( type = 'filter', target = ~'customdata', operation = '=', value = as.factor(unique(Datos$Nombre_Indicador)[1]))) ) %>% add_trace(type = 'scatter', mode='lines+markers', visible = 'legendonly' ) %>% layout(xaxis = xx, yaxis = yy, legend = list( font = list( size = 10, color = "#000"), orientation = 'v', x = -0.2, y = 0.9), updatemenus = list( list(direction = "right", xanchor = 'left', yanchor = "top", x = 0.0, y= 1.1, font = list( size = 10), type = 'dropdown', active = 0, buttons = apply(as.data.frame(as.factor(unique(Datos$Nombre_Indicador))), 1, function(x) list(method = 'restyle',args = list('transforms[0].value',x),label = x))) ) ) fig1 ``` Gráfica 3 ======================================================================= Row ----------------------------------------------------------------------- ### Indicadores de actualización por año, y por entidad federativa ```{r} fig2 <- plot_ly(Datos2, x = ~Edo, y = ~Indicador, customdata = ~año, color = ~Nombre_Indicador, type = 'bar', visible = 'legendonly', hoverinfo = 'text', text = ~paste(Edo, '
Indicador:', Nombre_Indicador, '
Porcentaje:', round(Indicador,2),'%', '
Limite inferior:', round(Limite_Inferior,2),'%', '
Limite superior:', round(Limite_Superior,2),'%'), transforms = list( list( type = 'filter', target = 'customdata', operation = '=', value = as.numeric(unique(Datos2$año)[1]) ))) %>% layout(barmode = 'group', xaxis = xx2, yaxis = yy, legend = list( font = list( size = 10, color = "#000"), orientation = 'h', x = 0.0, y = -0.2), updatemenus = list( list(direction = "right", xanchor = 'left', yanchor = "top", x = 0.0, y= 1.1, font = list( size = 10), type = 'dropdown', active = 0, buttons = apply(as.data.frame(as.numeric(unique(Datos2$año))), 1, function(x) list(method = 'restyle',args = list('transforms[0].value',x),label = x))) )) fig2 ```